package com.geticsen.leetcode;

import java.util.ArrayList;
import java.util.List;

//https://leetcode-cn.com/problems/combination-sum-ii/
public class Solution_40 {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        tryCombinationSum(res, target, 0, candidates, new ArrayList<>());
        return res;
    }

    private void tryCombinationSum(List<List<Integer>> res, int target, int index, int[] candidates, List<Integer> temp) {
        if (target == 0) {
            res.add(new ArrayList<>(temp));
            return;
        }
        if (target < 0) {
            return;
        }
        for (int i = index; i < candidates.length; i++) {
            temp.add(candidates[i]);
            tryCombinationSum(res, target - candidates[i], i, candidates, temp);
            temp.remove(new Integer(candidates[i]));
        }
    }
}
